from functools import wraps
from flask import session
from src.api.https import fail_api
from src.model import UserModel


def login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if session.get('uid') is None:  # 检查用户是否已登录
            print("未登录")
            return fail_api(msg="Unauthorized", code_id=401)
        return func(*args, **kwargs)

    return wrapper


def admin_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if UserModel.query.filter_by(uid=session.get('uid')).first().role <= 0:
            print("not admin")
            return fail_api(msg="Not Admin", code_id=401)
        return func(*args, **kwargs)

    return wrapper
